package com.autoEcole.service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.autoEcole.dao.EleveDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Eleve;

public class EleveService {
	
	private EleveDAO eleveDAO;
	/**
	 * EleveService
	 */
	private static EleveService eleveService;
	
	/**
	 * Constructor
	 */
	private EleveService() {
		super();
	}

	/**
	 * Returns the EleveService instance (Design Pattern : Singleton)
	 * 
	 * @return EleveService
	 */
	public static synchronized EleveService getInstance() {
		if (eleveService == null)
			eleveService = new EleveService();
		return eleveService;
	}

	public EleveDAO getEleveDAO() {
		return eleveDAO;
	}

	public void setEleveDAO(EleveDAO eleveDAO) {
		this.eleveDAO = eleveDAO;
	}

	public int create(Eleve eleve) throws ApplicationException {
		try {
			return eleveDAO.create(eleve);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(Eleve eleve) throws ApplicationException {
		try {
			eleveDAO.update(eleve);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Eleve findById(int id) throws ApplicationException {
		try {
			return eleveDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * @throws ParseException 
	 * 
	 */
	public List<Eleve> findAll() throws ApplicationException, ParseException {
		try {
			return eleveDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Eleve> findAllMap() throws ApplicationException {
		try {
			return eleveDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int eleveId) throws ApplicationException {
		try {
			eleveDAO.delete(eleveId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
}
